﻿using Entity;
using Entity.Front_Desk;
using Entity.ResultModel.Menu;
using IHotelBLL.IEmployeeBLL;
using IHotelDAL.IFrondeskDAL;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;

namespace HotelBLL.FrontdeskBLL
{
    public class MenuBLL : IMenuBLL
    {
        IMenuDAL _menuDAL;
        IFrontDeskDAL _frontDesk;
        IRoleDAL _roleDAL;
        IRole_MenuDAL _role_MenuDAL;
        IEmployeeRoleDAL _employeeRole;
        public MenuBLL(IMenuDAL menuDAL, IFrontDeskDAL frontDesk, IRoleDAL roleDAL, IRole_MenuDAL role_MenuDAL, IEmployeeRoleDAL employeeRole)
        {
            _menuDAL = menuDAL;
            _frontDesk = frontDesk;
            _roleDAL = roleDAL;
            _role_MenuDAL = role_MenuDAL;
            _employeeRole = employeeRole;
        }
        public bool AddMenu(MenuInfo menu, out string msg)
        {
            menu.Id = Guid.NewGuid().ToString();
            menu.CreateTime = DateTime.Now;
            bool isSuccess = _menuDAL.CreateEntity(menu);
            msg = isSuccess ? "添加成功" : "添加失败";
            return isSuccess;
        }
        public bool DeleteMenu(string id, out string msg)
        {
            MenuInfo menu = _menuDAL.GetEntities().Where(m => m.Id == id).FirstOrDefault();
            bool isSuccess = _menuDAL.DeleteEntity(menu);
            msg = isSuccess ? "删除成功" : "删除失败";
            return isSuccess;
        }
        public bool EditMenu(string id, string description, string title, string href, string parentId, int level, string icon, string target, int sort, out string msg)
        {
            MenuInfo menu = GetMenuInfoById(id);
            if (menu == null)
            {
                msg = "未找到菜单";
                return false;
            }
            menu.Description = description;
            menu.Title = title;
            menu.Href = href;
            menu.ParentId = parentId;
            menu.Level = level;
            menu.Icon = icon;
            menu.Target = target;
            menu.Sort = sort;
            if(href is null)
            {
                menu.Href = null;
            }
            bool isSuccess = _menuDAL.UpdateEntity(menu);
            msg = isSuccess ? "修改成功" : "修改失败";
            return isSuccess;
        }

        public MenuInfo GetMenuInfoById(string id)
        {
            return _menuDAL.GetEntities().Where(m => m.Id == id).FirstOrDefault();
        }
        public  List<SelectOptionModel> GetMenuOptions(string menuId)
        {
            return _menuDAL.GetEntities().Where(u => u.Id!=menuId && u.Level == 1).Select(d => new SelectOptionModel
            {
                Id = d.Id,
                Name = d.Title
            }).ToList();
        }
        public List<GetMenuInfo> GetMenuInfos(int page, int limit, out int count)
        {
            var datas = from m1 in _menuDAL.GetEntities()
                       join m2 in _menuDAL.GetEntities()
                       on m1.ParentId equals m2.Id into mmTemp
                        from mm in mmTemp.DefaultIfEmpty()
                        select new
                       {
                           m1.Id,
                           m1.Title,
                           m1.Description,
                           m1.CreateTime,
                           parentName = mm.Title,
                           m1.Target,
                           m1.ParentId,
                           m1.Icon,
                           m1.Href,
                           m1.Sort,
                           m1.Level
                       };
            count = datas.Count();
            return datas.Skip((page - 1) * limit).OrderBy(m => m.Sort).Take(limit).Select(m => new GetMenuInfo
            {
                Id = m.Id,
                Href = m.Href,
                Icon = m.Icon,
                Level = m.Level,
                ParentName = m.parentName,
                Sort = m.Sort,
                Title = m.Title,
                Target = m.Target,
                Description = m.Description,
            }).ToList();
        }
        public List<MenuModel> GetMenuInfos(string userId)
        {
            //获取数据所有的菜单
            var totalMenuDatas = _menuDAL.GetEntities().AsQueryable().OrderBy(m => m.Sort);
            var Frontdesk = _frontDesk.GetEntities().Where(f => f.Id == userId).FirstOrDefault();
            if (Frontdesk == null)
            {
                return null;
            }
            else if (Frontdesk.IsLeader == false)
            {
                var employeeIds = _employeeRole.GetEntities().Where(r => r.EmployeeId == userId).Select(r => r.RoleId);
                var menuIds = _role_MenuDAL.GetEntities().Where(r => employeeIds.Contains(r.RoleId)).Select(r => r.MenuId);
                totalMenuDatas = totalMenuDatas.Where(m => menuIds.Contains(m.Id)).OrderBy(m => m.Sort);
            }
            var total = totalMenuDatas.Select(m => new MenuModel
            {
                Title = m.Title,
                Icon = m.Icon,
                Href = m.Href,
                Target = m.Target,
                Level = m.Level,
                ParentId = m.ParentId,
                Id = m.Id
            }).ToList();
            var rootMenus = total.Where(m => m.Level == 1 && m.ParentId == null).Select(m => new MenuModel
            {
                Title = m.Title,
                Icon = m.Icon,
                Href = m.Href,
                Target = m.Target,
                Id = m.Id,
                ParentId = m.ParentId,
            }).ToList();
            RecursionMenu(rootMenus, total);
            return rootMenus;
        }
        #region 递归
        public void RecursionMenu(List<MenuModel> parentMenus, List<MenuModel> totalMenu)
        {
            foreach (var parentMenu in parentMenus)
            {
                //找到父级的自己菜单
                var childMenus = totalMenu.Where(mm => mm.ParentId == parentMenu.Id).Select(mm => new MenuModel
                {
                    Title = mm.Title,
                    Icon = mm.Icon,
                    Href = mm.Href,
                    Target = mm.Target,
                    Id = mm.Id
                }).ToList();

                if (childMenus.Count != 0)
                {
                    //递归继续找子集才的子集
                    RecursionMenu(childMenus, totalMenu);
                }
                parentMenu.Child = childMenus;
            }
        }

        #endregion
    }
}
